//
//  WillShowVC.m
//  MTime
//
//  Created by Carl_fang on 15/3/28.
//  Copyright (c) 2015年 Carl_fang. All rights reserved.
//

#import "WillShowVC.h"
#import "WillShowModel.h"
#import "willShowCell.h"

@interface WillShowVC () <NSURLConnectionDelegate,NSURLConnectionDataDelegate>


@property (nonatomic) NSInteger month;
@property (nonatomic, copy) NSString *str;


@property (nonatomic, retain) NSMutableData *receiveData;
@property (nonatomic, retain) NSMutableArray *moviecomingsArray;
@property (nonatomic, retain) NSMutableArray *attentionArray;

@property (nonatomic, retain) NSMutableArray *monthArray;
@property (nonatomic, retain) NSMutableArray *NmonthArray;
@property (nonatomic, retain) NSMutableArray *LmonthArray;



@end

@implementation WillShowVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    //self.tableView.tableFooterView = [UIView new];
    
    [self getMonth];
    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
    
    //    self.tableView.frame = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, self.view.frame.size.width, self.view.frame.size.height);
    
    
    //self.tableView.contentInset = UIEdgeInsetsMake(100, 0, 0, 0);
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 200)];
    view.backgroundColor = [UIColor blackColor];
    self.tableView.tableHeaderView = view;
    
    NSURL *url = [NSURL URLWithString:@"http://api.m.mtime.cn/Movie/MovieComingNew.api?locationId=292"];
    
    //    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    //
    //        [NSURLConnection connectionWithRequest:request delegate:self];
    self.receiveData = [NSMutableData data];
    self.moviecomingsArray = [NSMutableArray array];
    self.attentionArray = [NSMutableArray array];
    self.monthArray = [NSMutableArray array];
    self.NmonthArray = [NSMutableArray array];
    self.LmonthArray = [NSMutableArray array];
    
    
    
    NSDictionary *dict = [NSDictionary dictionary];
    
    dict = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfURL:url] options:NSJSONReadingMutableLeaves error:nil];
    
    NSArray *moviecomings = [NSArray array];
    moviecomings = dict[@"moviecomings"];
    
    NSArray *attention = [NSMutableArray array];
    attention = dict[@"attention"];
    
    
    for (NSDictionary *dic in moviecomings) {
        
        WillShowModel *willShowModel = [WillShowModel WillShowModelWithDict:dic];
        
        [_moviecomingsArray addObject:willShowModel];
        if (willShowModel.rMonth == _month) {
            
            
            [_monthArray addObject:willShowModel];
            
            
        }else if (willShowModel.rMonth == (_month + 1)) {
            
            
            [_NmonthArray addObject:willShowModel];
        }else
        {
            [_LmonthArray addObject:willShowModel];
        }
        
        for (NSDictionary *dic in attention) {
            
            WillShowModel *attention = [WillShowModel WillShowModelWithDict:dic];
            
            [_attentionArray addObject:attention];
        }
        
        if (_moviecomingsArray.count != 0 && _attentionArray.count != 0) {
            self.tableView.delegate = self;
            self.tableView.dataSource = self;
        }
    }
    
    
    
    
}

- (NSString *)getMonth
{
    
    NSDate *date = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSCalendarUnitMonth | NSCalendarUnitDay;
    NSDateComponents *dateCompoent = [calendar components:unitFlags fromDate:date];
    _str = [NSString stringWithFormat:@"%d",[dateCompoent month]];
    _month = [dateCompoent month];
    
    return _str;
}


/*
 
 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
 {
 NSLog(@"%@",error);
 }
 
 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
 {
 [self.receiveData appendData:data];
 }
 
 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
 {
 self.receiveData = [NSMutableData data];
 self.moviecomingsArray = [NSMutableArray array];
 self.attentionArray = [NSMutableArray array];
 self.monthArray = [NSMutableArray array];
 self.NmonthArray = [NSMutableArray array];
 self.LmonthArray = [NSMutableArray array];
 
 
 }
 
 
 
 - (void)connectionDidFinishLoading:(NSURLConnection *)connection
 {
 
 if (_monthArray != nil) {
 
 NSDictionary *dict = [NSDictionary dictionary];
 
 dict = [NSJSONSerialization JSONObjectWithData:_receiveData options:NSJSONReadingAllowFragments error:nil];
 
 NSArray *moviecomings = [NSArray array];
 moviecomings = dict[@"moviecomings"];
 
 NSArray *attention = [NSMutableArray array];
 attention = dict[@"attention"];
 
 
 for (NSDictionary *dic in moviecomings) {
 
 WillShowModel *willShowModel = [WillShowModel WillShowModelWithDict:dic];
 
 [_moviecomingsArray addObject:willShowModel];
 if (willShowModel.rMonth == _month) {
 
 
 [_monthArray addObject:willShowModel];
 
 
 }else if (willShowModel.rMonth == (_month + 1)) {
 
 
 [_NmonthArray addObject:willShowModel];
 }else
 {
 [_LmonthArray addObject:willShowModel];
 }
 
 
 }
 
 for (NSDictionary *dic in attention) {
 
 WillShowModel *attention = [WillShowModel WillShowModelWithDict:dic];
 
 [_attentionArray addObject:attention];
 }
 
 if (_moviecomingsArray.count != 0 && _attentionArray.count != 0) {
 self.tableView.delegate = self;
 self.tableView.dataSource = self;
 }
 }
 
 
 
 
 }
 */

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    if (section == 0) {
        return _monthArray.count;
    }else if(section == 1)
    {
        return _NmonthArray.count;
    }else
    {
        return _LmonthArray.count;
    }
    
}


//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
//{
//    return 0;
//}

//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return 20;
//}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 140;
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    //return @"123";
    
    NSString *tempN = [NSString stringWithFormat:@"%d月",_month];
    NSString *tempO = [NSString stringWithFormat:@"%d月",_month + 1];
    NSString *tempT = [NSString stringWithFormat:@"%d月",_month + 2];
    switch (section) {
        case 0:
        {
            
            return tempN;
            
        }
            break;
        case 1:
        {
            return tempO;
        }
            break;
        case 2:
        {
            return tempT;
        }
            break;
    }
    return [self getMonth];
}

//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//
//    //NSLog(@"%ld",_month);
//    switch (section) {
//        case 0:
//        {
//
//            return [self returnHeadView:_month];
//
//        }
//            break;
//        case 1:
//        {
//            return [self returnHeadView:_month + 1];
//        }
//            break;
//        case 2:
//        {
//            return [self returnHeadView:_month + 2];
//        }
//            break;
//    }
//    return [self returnHeadView:_month];
//}

- (UIView *)returnHeadView:(NSInteger)month
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 80)];
    //view.backgroundColor = [UIColor orangeColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 20)];
    NSString *str = [NSString stringWithFormat:@"0%d月",month];
    label.text = str;
    label.backgroundColor = [UIColor lightGrayColor];
    [view addSubview:label];
    
    return view;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *ID = @"cell";
    
    willShowCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [[willShowCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
    }
    switch (indexPath.section) {
        case 0:
        {
            cell.model = _monthArray[indexPath.row];
            
        }
            break;
        case 1:
        {
            cell.model = _NmonthArray[indexPath.row];
        }
            break;
        case 2:
        {
            cell.model = _LmonthArray[indexPath.row];
        }
            break;
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    return cell;
}

/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */

/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
 } else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }
 }
 */

/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
